405. 数字转换为十六进制数
为保证权益,题目请参考 405. 数字转换为十六进制数(From LeetCode).
解决方案1
Python
python
# 405. 数字转换为十六进制数
# https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/
################################################################################
def bin2hex(num: int) -> str:
if num < 10:
return str(num)
elif num == 10:
return "a"
elif num == 11:
return "b"
elif num == 12:
return "c"
elif num == 13:
return "d"
elif num == 14:
return "e"
elif num == 15:
return "f"
else:
return ""
class Solution:
def toHex(self, num: int) -> str:
if num == 0:
return "0"
else:
if num < 0:
num = num & 0xFFFFFFFF
ans = []
while num != 0:
a, b = divmod(num, 16)
ans.append(bin2hex(b))
num = a
ans = "".join(reversed(ans))
return ans
################################################################################
if __name__ == "__main__":
solution = Solution()
print(solution.toHex(-1))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48